<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="" prop="customerBy">
        <el-select
          v-model="queryParams.customerBy"
          :placeholder="$t('pleaseSelect')"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in customerUserList"
            :key="item.userId"
            :label="item.userName"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          :placeholder="$t('pleaseEnterYourMobileNumber')"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="userName">
        <el-input
          v-model="queryParams.userName"
          :placeholder="$t('enterOneUserName') "
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          :placeholder="$t('pleaseEnterTheOrderNumber') "
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="overStatus">
        <el-select
          v-model="queryParams.overStatus"
          :placeholder="$t('isOver')"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in overStatusList"
            :key="item.value"
            :label="checkName(item.label)"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="applyType">
        <el-select
          v-model="queryParams.applyType"
          :placeholder="$t('applyType')"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in applyTypeList"
            :key="item.value"
            :label="checkName(item.label)"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="" prop="isRemark">
        <el-select
          v-model="queryParams.isRemark"
          placeholder="是否备注"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="item in overStatusList"
            :key="item.value"
            :label="checkName(item.label)"
            :value="item.value"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :start-placeholder="$t('distributionStartTime')"
          :end-placeholder="$t('distributionEndTime')"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="序号" align="center" prop="id"/>
      <el-table-column :label="$t('platform')" align="center" prop="companyId"/>
      <el-table-column :label="$t('orderNumber')" align="center" prop="orderSn"/>
      <el-table-column :label="$t('userName')" align="center" prop="userName"/>
      <el-table-column :label="$t('phoneNumber')" align="center" prop="userPhone"/>
      <el-table-column :label="$t('loanAmount')" align="center" prop="realApplyMoney"/>
      <el-table-column :label="$t('manageFee')" align="center" prop="manageMoney"/>
      <el-table-column :label="$t('examineFee')" align="center" prop="riskMoney"/>
      <el-table-column :label="$t('serviceFee')" align="center" prop="applyServiceCharge"/>
      <el-table-column :label="$t('actualAmount')" align="center" prop="realApplyMoney">
        <template slot-scope="scope">
          {{ scope.row.realApplyMoney - scope.row.manageMoney - scope.row.riskMoney - scope.row.applyServiceCharge }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('firstExamine')" align="center" prop="oneExamineBy">
        <template slot-scope="scope">
          <span v-for="item in userList" v-if="scope.row.oneExamineBy == item.userId">{{ item.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('twoExamine')" align="center" prop="twoExamineBy">
        <template slot-scope="scope">
          <span v-for="item in userList" v-if="scope.row.twoExamineBy == item.userId">{{ item.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('customer')" align="center" prop="customerBy">
        <template slot-scope="scope">
          <span v-for="item in customerUserList" v-if="scope.row.customerBy == item.userId">{{ item.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('status')" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == '4'">{{ $t('toBeWithdrawn') }}</span>
          <span v-else>{{ $t('unknown') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('withdrawalRemarks')" align="center" prop="customerRemark"/>
      <el-table-column label="备注时间" align="center" prop="customerRemarkTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.customerRemarkTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('timeOfApplication')" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('operate')"
        align="center"
        width="160"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAddWithdrawal(scope.row)"
          >添加备注
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
          >
            <router-link :to="{path:'/app/user-info/index/' + scope.row.id + '/' + scope.row.userId,query:queryParams}"
                         class="link-type">
              <span>{{ $t('see') }}</span>
            </router-link>
          </el-button>

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handlePay(scope.row,'0')"-->
          <!--            v-hasPermi="['app:apply:update']"-->
          <!--          >线下打款-->
          <!--          </el-button>-->

          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handlePay(scope.row,'1')"-->
          <!--            v-hasPermi="['app:apply:update']"-->
          <!--          >线上打款-->
          <!--          </el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >{{ $t('close') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 分配 -->
    <el-dialog :title="$t('loan')" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item :label="$t('loanLimit')">
          <el-input v-model="form.applyMoney" disabled/>
        </el-form-item>

        <el-form-item :label="$t('cycle')">
          <el-input v-model="form.applyCycle" disabled/>
        </el-form-item>

        <el-form-item :label="$t('manageFee')">
          <el-input v-model="form.manageMoney" disabled/>
        </el-form-item>

        <el-form-item :label="$t('serviceFee')">
          <el-input v-model="form.applyServiceCharge" disabled/>
        </el-form-item>

        <el-form-item :label="$t('examineFee')">
          <el-input v-model="form.riskMoney" disabled/>
        </el-form-item>

        <el-form-item :label="$t('actualAmount')">
          <el-input v-model="form.realPayMoney" disabled/>
        </el-form-item>

        <el-form-item :label="$t('dayRate')">
          <el-input v-model="form.dayRate" disabled>
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item :label="$t('overRate')">
          <el-input v-model="form.expireRate" disabled>
            <template slot="append">%</template>
          </el-input>
        </el-form-item>

        <el-form-item :label="$t('bankSerialNumber')" prop="bankOrderNo">
          <el-input v-model="form.bankOrderNo" :placeholder="$t('bankSerialNumber')"/>
        </el-form-item>

        <el-form-item :label="$t('remarks')" prop="remark">
          <el-input type="textarea" :rows="3" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitHandlerPay">{{ $t('examineStatus.refuse') }}</el-button>
        <el-button @click="open = false">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog title="提现备注" :visible.sync="overOpen" width="600px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="120px">
        <el-form-item :label="$t('details')" prop="detail">
          <el-input type="textarea" v-model="form1.detail"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitWithdrawalLogHandler">{{ $t('confirm') }}</el-button>
        <el-button @click="overOpen = false">{{ $t('cancel') }}</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listWaitWithdralApply, listExamineUser, closeApply, handlePay, addOverLogForm} from "@/api/app/apply";
import {listCustomerUser} from "@/api/system/user";

export default {
  name: "WaitWithdrawal",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      applyList: null,
      userList: null,
      customerUserList: null,
      // 公司数据
      companyList: null,
      companyId: this.$store.state.user.companyId,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      overOpen: false,
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      form1: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerBy: undefined,
        overStatus: undefined,
        applyType: undefined,
        userName: undefined,
        userPhone: undefined,
        orderSn: undefined,
        isRemark: undefined,
        status: '4',
      },
      overStatusList: [
        {
          value: '0',
          label: 'no'
        },
        {
          value: '1',
          label: 'yes'
        },
      ],

      applyTypeList: [
        {
          value: 0,
          label: 'firstApply'
        },
        {
          value: 1,
          label: 'applyOne'
        },
        {
          value: 2,
          label: 'applyTwo'
        },
        {
          value: 3,
          label: 'applyThree'
        }, {
          value: 4,
          label: 'applyFour'
        },
        {
          value: 5,
          label: 'applyFive'
        },
        {
          value: 6,
          label: 'applySix'
        },
        {
          value: 7,
          label: 'applySeven'
        },
      ],
      // 表单校验
      rules: {
        bankOrderNo: [
          {required: true, message: "银行流水号不能为空", trigger: "blur"},
        ]
      },
      rules1: {
        detail: [
          {required: true, message: "备注不能为空", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    if (this.$route.query && this.$route.query.pageNum) {
      this.queryParams.pageNum = parseInt(this.$route.query.pageNum);
      this.queryParams.pageSize = parseInt(this.$route.query.pageSize);
      this.queryParams.userName = this.$route.query.userName;
      this.queryParams.userPhone = this.$route.query.userPhone;
      this.queryParams.orderSn = this.$route.query.orderSn;
      this.queryParams.overStatus = this.$route.query.overStatus;
      this.queryParams.applyType = this.$route.query.applyType ? parseInt(this.$route.query.applyType) : undefined;
      this.queryParams.customerBy = this.$route.query.customerBy;
      this.queryParams.isRemark = this.$route.query.isRemark;
    }
    this.getList();
    this.getListExamineUser();
    this.getListCustomerUser();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      if (this.companyId) {
        this.queryParams.companyId = this.companyId
      }
      listWaitWithdralApply(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.applyList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    checkName(name) {
      if (this.$te(name)) {
        return this.$t(name)
      }
      return name;
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        realApplyMoney: 0,
        manageMoney: 0,
        riskMoney: 0,
        applyServiceCharge: 0,
        applyCycle: 0,
        realPayMoney: 0,
        bankOrderNo: undefined
      };
      this.form1 = {
        id: undefined,
        detail: undefined
      }
      this.resetForm("form");
    },

    getListExamineUser() {
      listExamineUser().then(response => {
          this.userList = response.data;
        }
      );
    },
    getListCustomerUser() {
      listCustomerUser().then(response => {
          this.customerUserList = response.data;
        }
      );
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    handleAddWithdrawal(row) {
      this.reset();
      this.form1.id = row.id;
      this.overOpen = true;
    },
    submitWithdrawalLogHandler() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          addOverLogForm(this.form1).then(response => {
            this.overOpen = false;
            this.getList();
          });
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getApply(id).then(response => {
        this.form = response.data;
        this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
        this.open = true;
        this.title = "修改";
      });
    },


    /** 提交按钮 */
    submitForm: function () {
      // this.$refs["form"].validate(valid => {
      //   if (valid) {
      //     if (this.form.id != undefined) {
      //       updateIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("修改成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     } else {
      //       addIndexApply(this.form).then(response => {
      //         this.$modal.msgSuccess("新增成功");
      //         this.open = false;
      //         this.getList();
      //       });
      //     }
      //   }
      // });
    },

    /** 打款 */
    handlePay(row) {
      this.reset();
      this.form = row;
      this.form.realPayMoney = this.form.realApplyMoney - this.form.manageMoney - this.form.riskMoney - this.form.applyServiceCharge;
      this.open = true;

    },

    submitHandlerPay() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let that = this.form;
          this.$modal.confirm(this.$t('isLoan')).then(function () {
            return handlePay(that.id, '0', that.bankOrderNo);
          }).then(() => {
            this.getList();
            this.$modal.msgSuccess(this.$t('loanSuccess'));
          }).catch((e) => {
            this.$modal.msgSuccess(this.$t('loanFail') + e);
          });
        }
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('closed')).then(function () {
        return closeApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("success");
      }).catch(() => {
      });
    },

    // handleMoney(){
    //   this.form.realMoney = this.form.applyMoney - this.form.manageMoney - this.form.examineMoney - this.form.serviceMoney;
    // }
  }
};
</script>
